local Util = require("common.Util")
local CombatDefine = require("combat.CombatDefine")
local SkillExcel = require("excel.skill").skill
local BufferExcel = require("excel.buffer").buffer
local MoshouExcel = require("excel.moshou")
local MoshouGrid = require("moshou.MoshouGrid")
local CombatImpl = require("combat.CombatImpl")
local MoshouLogic = require("moshou.MoshouLogic")

local function calcHurt(skillConf,lv)
	local hurt = skillConf.hurtRate[1]
	local hurtRateGrow = skillConf.hurtRateGrow
	hurt = hurt + hurtRateGrow * (lv - 1)
	return hurt
end

local function calcArgs(skillConf,lv)
	local args = Util.copyTable(skillConf.args)
	local argsGrow = skillConf.argsGrow
	local cmd = skillConf.cmd 
    if #argsGrow > 0 then
	    if cmd == "attr" then
	    	for i = 1,#args do
	    		local v1 = args[i]
	    		local v2 = argsGrow[i]
	    		v1[2] = v1[2] + math.ceil((lv / v2[2]) * v2[1])
	    	end
	    elseif cmd == "hp" or cmd == "hpHurt" or cmd == "hurt" or cmd == "jobHurt" or cmd == "jobHurt" then
	    	local v2 = argsGrow[1]
	    	args[2] = args[2] + math.ceil((lv / v2[2]) * v2[1])
	    elseif cmd == "mp" then
	    	local v2 = argsGrow[1]
	    	args[1] = args[1] + math.ceil((lv / v2[2]) * v2[1])
	    end
    end
	return args
end

function calcGrowArgs(pet)
	local skillID = pet.skillList[1][1]
	local skillConf = SkillExcel[skillID]

	if skillConf.type == CombatDefine.SKILL_TYPE4 then
		pet.hurt = calcHurt(skillConf, pet.lv)
		pet.args = calcArgs(skillConf, pet.lv)
	end
end


function clacBuffArgs(pet)
	local skillID = pet.skillList[1][1]
	local skillConf = SkillExcel[skillID]
	if not skillConf then return end
	pet.buffers = pet.buffers or {}
	local lv = pet.lv 
	local len = 0
	for i = 1,#skillConf.buffers do
		local bufLv = lv - 1
		local bufferId = skillConf.buffers[i][1]
		local buffConf = BufferExcel[bufferId]
		if buffConf and #buffConf.args > 0 then			
			pet.buffers[bufferId] = pet.buffers[bufferId] or {}
			if buffConf.cmd == "attr" or buffConf.cmd == "attrQusan" then
				local bugArg = {}
				for j = 1,#buffConf.args do 
					local attrId = buffConf.args[j][1]
					local val = buffConf.args[j][2]
					local growRate = 0

					if buffConf.argsGrow[j] then
						local growLv =  buffConf.argsGrow[j][2]
						if bufLv >= growLv then						
							growRate = buffConf.argsGrow[j][1]
							growRate = math.floor(bufLv/growLv) * growRate
						end
						table.insert(bugArg, {attrId,growRate+val})
					end
				end
				pet.buffers[bufferId].args = bugArg 
				len = len + 1 
			elseif #buffConf.args > 0 then
				local bugArg = {}
				local attrId = buffConf.args[1]
				local val = buffConf.args[2] or 0
				local growRate = 0
				if #buffConf.argsGrow > 0 then
                    local growLv = buffConf.argsGrow[1][2]
                    if bufLv >= growLv then						
                        growRate = buffConf.argsGrow[1][1]
                        growRate = math.floor(bufLv/growLv) * growRate
                    end
                end    
				--print(" bufferId attrId , val + growRate ", bufferId , attrId, val + growRate)
				bugArg = {attrId, val + growRate}
				pet.buffers[bufferId].args = bugArg
				len = len + 1 
			end
		end
	end
	pet.buffers[0] = len
end

